<template>
  <el-dialog
    title="请选择图片"
    :visible.sync="dialogFormVisible"
    center
    width="1187.9px"
    :before-close="handleDialogClose"
  >
    <div class="filter-container" style="text-align:center">
      <el-input
        clearable
        class="filter-item"
        style="width: 200px;"
        v-model="keyword"
        placeholder="请输入分类名称"
      ></el-input>
      <el-button type="primary" icon="el-icon-search" @click="handleFind">查找</el-button>
      <el-button type="primary" @click="handleRest" icon="el-icon-refresh">重置</el-button>
    </div>

    <el-tabs tab-position="left" v-loading="Loading" v-model="tagActiveIndex" @tab-click="clickTab">
      <el-tab-pane
        :label="pictureSort.name"
        v-for="pictureSort in  pictureSorts"
        :key="pictureSort.uid"
      >
        <div v-for="picture in pictures" :key="picture.fileUid" style="display: inline-block;float:left">
          <el-image
            :class="checkedFileUidList.indexOf(picture.fileUid)!=-1? 'checkedPic':''"
            @click="checkPic(picture.fileUid,picture.pictureUrl)"
            :src="VUE_APP_IMAGE_URL + picture.pictureUrl"
            style="cursor:pointer"
            class="showPicture"
            fit="cover"
          />
        </div>

        <el-image
          :src="imgUrl"
          style="cursor:pointer;display: inline-block;float:left"
          class="showPicture"
          v-if="isShowUpload"
          fit="cover"
          @click="toPictureManager(pictureSort.uid)"
        />

      </el-tab-pane>

    </el-tabs>
    <el-pagination
          style="text-align: center;"
          @current-change="handleCurrentChange"
          :current-page.sync="currentPage"
          :page-size="pageSize"
          small
          layout="prev, pager, next"
          :total="total%pageSize==0?total+1:total"
        ></el-pagination>
        <div style="text-align: end;">
          <el-button @click="handleDialogClose" icon="el-icon-close" circle></el-button>
          <el-button @click="commit" type="success" icon="el-icon-check" circle></el-button>
        </div>
  </el-dialog>
</template>

<script>
import { getPictureSortList, getPictureSortByUid } from "@/api/pictureSort";
import { getPictureList } from "@/api/picture";
import Vue from "vue";
export default {
  props: ["photoVisible", "limit"],
  data() {
    return {
      Loading: true,
      imgUrl: require("@/assets/upload.png"),
      VUE_APP_IMAGE_URL: process.env.VUE_APP_IMAGE_URL,
      keyword: "",
      pictureSortUid: "", //相册分类uid
      pictureSorts: [],
      pictures: [],
      checkedFileUidList: [], //选择的图片uids
      checkedPictureUrlList: [], //选择的图片urls
      currentPage: 1,
      pageSize: 25,
      total: 0, //总数量
      tagActiveIndex: "",
      dialogFormVisible: false,
      isShowUpload: false,
      limitCount: this.limit,
    };
  },
  computed: {},
  created() {
    // TODO 图片初始化
    // this.pictureSortList();
  },
  watch: {
    photoVisible() {
      this.dialogFormVisible = this.photoVisible;
    },
  },

  methods: {
    checkPic(fileUid, pictureUrl) {
      if (this.checkedFileUidList.indexOf(fileUid) != -1) {
        this.checkedFileUidList.splice(
          this.checkedFileUidList.indexOf(fileUid),
          1
        );
        this.checkedPictureUrlList.splice(
          this.checkedPictureUrlList.indexOf(pictureUrl),
          1
        );
      } else {
        if (this.checkedFileUidList.length >= this.limitCount) {
          this.$message({
            message: "最多只能选择" + this.limitCount + "张图片！",
            type: "error",
          });
          return;
        }
        this.checkedFileUidList.push(fileUid);
        this.checkedPictureUrlList.push(pictureUrl);
      }
    },
    pictureList() {
      this.Loading = true;
      var params = {};
      params.pictureSortUid = this.pictureSortUid;
      params.pageSize = this.pageSize;
      params.currentPage = this.currentPage;
      getPictureList(params).then((response) => {
          this.pictures = response.data.records;
          this.currentPage = response.data.current;
          this.pageSize = response.data.size;
          this.total = response.data.total;
          this.isShowUpload = this.total - this.pageSize * this.currentPage < 0;
          this.Loading = false;
      });
    },
    handleFind() {
      this.pictureSortList();
    },
    handleRest() {
      this.checkedFileUidList = [];
      this.pictureList();
      this.$message({
        message: "重置成功",
        type: "success",
      });
    },
    pictureSortList() {
      var params = {};
      params.pageSize = 9999;
      params.currentPage = 1;
      params.keyword = this.keyword;
      params.isShow = 1;
      getPictureSortList(params).then((response) => {
        this.pictureSorts = response.data.records;
        if (this.pictureSorts.length > 0) {
          this.pictureSortUid = this.pictureSorts[0].uid;
          this.pictureList();
        }
      });
    },
    handleDialogClose() {
      this.checkedFileUidList = [];
      this.checkedPictureUrlList = [];
      this.$emit("cancelModel", "");
    },
    clickTab() {
      //点击tag的回调
      this.currentPage = 1;
      this.pictureSortUid = this.pictureSorts[this.tagActiveIndex].uid;
      this.pictureList();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.pictureList();
    },
    commit() {
      if (this.checkedFileUidList.length < 1) {
        this.$message({
          message: "请选择图片！",
          type: "error",
        });
        return;
      }
      let data = {
        checkedFileUids: this.checkedFileUidList.join(","),
        checkedPictureUrlList: this.checkedPictureUrlList,
      };
      this.$emit("choose_data", data);
      this.handleDialogClose();
    },
    toPictureManager(pictureSortUid) {
      this.$confirm("是否跳转到图片管理进行上传？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$router.push({
            path: "/picture/picture",
            query: { pictureSortUid: pictureSortUid },
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消跳转",
          });
        });
    },
  },
};
</script>
<style scoped>
.showPicture {
  margin-right: auto;
  display: inline-block;
  width: 195px;
  height: 105px;
  margin: 5px;
  border: solid 1px #c7aeae;
}

.checkedPic {
  box-shadow: 0 0 0 4px #00c09e;
}

</style>
